<?php
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class WorkgroupController extends AdminBaseController {

    //组列表
    public function workgrouplist(){
    	$group = M("workgroup");
    	$list = $group->select();
    	$data['count'] = count($list);
        $user = new \Admin\Controller\UserController();
        for ($i=0; $i < $data['count']; $i++) { 
            $list[$i]['count'] = $user->getusersbygpid($list[$i]['id']);
            $list[$i]['stacount'] = $user->getstauserbygpid($list[$i]['id']);
        }
    	$this->assign('group',$list);
    	$this->assign('data',$data);
    	$this->display("./Public/AdminLTE/pages/tables/workgrouplist.html");
    }

    //ajax获取所有组及组空闲人数
    public function getgroupstas(){
        $group = M("workgroup");
        $list = $group->select();
        $user = new \Admin\Controller\UserController();
        for ($i=0; $i < count($list); $i++) {
            $list[$i]['stacount'] = $user->getstauserbygpid($list[$i]['id']);
        }
        $this->ajaxReturn($list);
    }

    //组成员列表
    public function getusers($id=0){
    	$user = M("user");
    	$list = $user->where('workid='.$id.' and state=1')->order()->select();
    	$data['count'] = count($list);
    	$data['name'] = $this->getnamebyid($id);
    	$this->assign('user',$list);
    	$this->assign('data',$data);
    	$this->display("./Public/AdminLTE/pages/tables/workgroupusers.html");
    }

    //根据组ID获取组员工
    public function getuserbygroup($id=0){
    	$user = M("user");
    	$list = $user->where('workid='.$id.' and state=1 and worksta=1')->order()->select();
    	$this->ajaxReturn($list);
    }

    //添加组
    public function add(){
    	$group = M("workgroup");
    	if (I('post.name')!="") {
    		$data['name'] = I('post.name');
    	}else{
    		$this->error();
    	}
    	$res = $group->field('name')->data($data)->add();
    	if ($res) {
    		$this->success('新增成功');
    	}else{
    		$this->error();
    	}
    }

    //根据ID获取组名
    public function getnamebyid($id=0){
    	$group = M("workgroup");
    	$name = $group->where('id='.$id)->getField('name');
    	return $name;
    }

    //获取所有组
    public function getallgroup(){
    	$group = M("workgroup");
    	$list = $group->select();
    	return $list;
    }
}